package com.kid.keep.controller.impl;

import com.kid.keep.common.SessionStore;
import com.kid.keep.common.UserSubject;
import com.kid.keep.controller.ICheckReportController;
import com.kid.keep.dto.*;
import com.kid.keep.mapper.ICheckItemCovertMapper;
import com.kid.keep.mapper.ICheckReportCovertMapper;
import com.kid.keep.po.CheckItemReport;
import com.kid.keep.request.AddCheckItemResultRequest;
import com.kid.keep.service.ICheckReportService;
import com.kid.keep.vo.*;
import com.honeybee.cloud.framework.common.response.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 检测报告
 * @Author JOHN
 * @Create 2020-01-06
 * @Since 1.0.0
 */
@RestController
public class CheckReportController implements ICheckReportController {

    private ICheckReportService checkReportService;
    private ICheckReportCovertMapper checkReportCovertMapper;
    private ICheckItemCovertMapper checkItemCovertMapper;

    @Autowired
    public void setCheckItemCovertMapper(ICheckItemCovertMapper checkItemCovertMapper) {
        this.checkItemCovertMapper = checkItemCovertMapper;
    }

    @Autowired
    public void setChildCheckItemMapper(ICheckReportCovertMapper checkReportCovertMapper) {
        this.checkReportCovertMapper = checkReportCovertMapper;
    }

    @Autowired
    public CheckReportController(ICheckReportService checkReportService) {
        this.checkReportService = checkReportService;
    }

    @Override
    public ResponseEntity<CheckItemReportDetailVO> getCheckReport(Long reportId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        CheckItemReportDetailDTO detailDTO = checkReportService.findByIdAndCreatorId(reportId, userSubject.getUserId());
        return ResponseEntity.ok(checkReportCovertMapper.toVO(detailDTO));
    }

    @Override
    public ResponseEntity<CheckItemReportDetailVO> startCheckReportAgain(Long reportId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        CheckItemReportDTO detailDTO = checkReportService.updateResultByReportId(reportId, userSubject.getUserId());
        return ResponseEntity.ok(checkReportCovertMapper.toVO(detailDTO));
    }

    @Override
    public ResponseEntity<List<ChildCheckItemVO>> getCheckItems(Long reportId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        List<ChildCheckItemDTO> checkItemVOS = checkReportService.findByReportIdAndCreatorId(reportId, userSubject.getUserId());
        return ResponseEntity.ok(checkItemCovertMapper.toVO(checkItemVOS));
    }

    @Override
    public ResponseEntity<ChildCheckItemVO> getCheckItemInfo(Long reportId, Long itemId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        ChildCheckItemDTO childCheckItemDTO = checkReportService.findByReportIdAndCreatorIdAndItemId(reportId, userSubject.getUserId(), itemId);
        return ResponseEntity.ok(checkItemCovertMapper.toCheckItemVO(childCheckItemDTO));
    }

    @Override
    public ResponseEntity<Integer> countCheckReport() {
        UserSubject userSubject = SessionStore.getSessionUser();
        int number = checkReportService.countCheckReport(userSubject.getUserId());
        return ResponseEntity.ok(number);
    }

    @Override
    public ResponseEntity<List<CheckItemReportStatisticsDetailVO>> countCheckReportByDateType(Integer type) {
        UserSubject userSubject = SessionStore.getSessionUser();
        List<CheckItemReportDTO> checkItemReportList = checkReportService.listCheckReportByDateType(type, userSubject.getUserId());
        return ResponseEntity.ok(checkReportCovertMapper.toStatisticsVO(checkItemReportList));
    }

    @Override
    public ResponseEntity<AddChildCheckItemResultVO> addCheckItemResult(Long reportId, Long itemId, AddCheckItemResultRequest addCheckItemResultRequest) {
        AddCheckItemResultDTO addCheckItemResultDTO = checkItemCovertMapper.toDTO(addCheckItemResultRequest, itemId, reportId);
        UserSubject userSubject = SessionStore.getSessionUser();
        AddChildCheckItemResultDTO childCheckItemDTO = checkReportService.addCheckItemResult(addCheckItemResultDTO, userSubject.getUserId());
        return ResponseEntity.ok(checkItemCovertMapper.toVO(childCheckItemDTO));
    }

    @Override
    public ResponseEntity<ChildCheckItemResultDetailDTO> getCheckItemResult(Long reportId, Long itemId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        ChildCheckItemResultDetailDTO childCheckItemDTO = checkReportService.getCheckItemResult(reportId, itemId, userSubject.getUserId());
        return ResponseEntity.ok(checkItemCovertMapper.toVO(childCheckItemDTO));
    }

    @Override
    public ResponseEntity<CheckItemReportVO> generateCheckItemReport(Long reportId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        CheckItemReportDTO checkItemReportDTO = checkReportService.generateCheckItemReport(reportId, userSubject.getUserId());
        return ResponseEntity.ok(checkItemCovertMapper.toReportVO(checkItemReportDTO));
    }

}
